﻿/* Author:
 *  Alessandro Pulidori
 */

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace TripLOAMobile.GPS
{
    public class NMEAParser
    {
        private string[] fields;
        private GPS gps;
        public NMEAParser(GPS gps)
        {
            this.gps = gps;
        }
        public bool Parse(string sentence)
        {
            fields = GetFields(sentence);
            switch (fields[0])
            {
                case "$GPRMC":
                    return ParseGPRMC(sentence);
                case "$GPGLL":
                    return ParseGPGLL(sentence);
                default:
                    return false;
            }
        }

        private bool ParseGPGLL(string sentence)
        {
            string[] m_temp = fields[6].Split('*');
            string m_checksum = m_temp[1];
            if (GetChecksum(sentence).Equals(m_checksum))
            {
                string[] temp = fields[1].Split('.');
                gps.Position.dblLatitude = Convert.ToDouble(temp[0] + ',' + temp[1]);
                temp = fields[3].Split('.');
                gps.Position.dblLongitude = Convert.ToDouble(temp[0] + ',' + temp[1]);
                return true;
            }
            else return false;
        }

        private bool ParseGPRMC(string sentence)
        {
            string[] m_temp = fields[11].Split('*');
            string m_checksum = m_temp[1];
            if (GetChecksum(sentence).Equals(m_checksum))
            {
                string[] temp = fields[3].Split('.');
                gps.Position.dblLatitude = Convert.ToDouble(temp[0] + ',' + temp[1]);
                temp = fields[5].Split('.');
                gps.Position.dblLongitude = Convert.ToDouble(temp[0] + ',' + temp[1]);
                return true;
            }
            else return false;
        }

        private string[] GetFields(string sentence)
        {
            return sentence.Split(',');
        }

        private string GetChecksum(string sentence)
        {
            char[] m_characters = sentence.ToCharArray();
            int m_checksum = 0;
            bool m_end = false;
            for (int i = 0; i < m_characters.Length & !m_end; i++)
            {
                switch (m_characters[i])
                {
                    case '$':
                        break;
                    case '*':
                        m_end = true;
                        break;
                    default:
                        {
                            if (m_checksum == 0)
                                m_checksum = Convert.ToByte(m_characters[i]);
                            else
                                m_checksum ^= Convert.ToByte(m_characters[i]);
                            break;
                        }
                }
            }
            return m_checksum.ToString("X");
        }
    }
}